#include "include.h"
static const char *TAG = "JSON";
/*
    当前设备状态
*/
extern tencent_shadow_device_info esp32_state;

/*
    函数功能：构建json格式影子数据字符串
    函数名字：build_shadow_string
    输出：*out_string       输出无格式json字符串
    输入：len              *out_string缓存长度
    返回：成功  true    失败    false
*/
int build_shadow_string(char *out_string, int len)
{
    char *cjson_str = NULL;

    cJSON *json_shadow = cJSON_CreateObject();
    cJSON *json_state = cJSON_CreateObject();
    cJSON *json_reported = cJSON_CreateObject();

    cJSON_AddItemToObject(json_shadow, "type", cJSON_CreateString("update"));
    cJSON_AddItemToObject(json_shadow, "version", cJSON_CreateNumber(0));
    cJSON_AddItemToObject(json_shadow, "clientToken", cJSON_CreateString("clientToken"));

    cJSON_AddItemToObject(json_reported, "humidity", cJSON_CreateNumber(esp32_state.humidity));
    cJSON_AddItemToObject(json_reported, "light", cJSON_CreateNumber(esp32_state.light));
    cJSON_AddItemToObject(json_reported, "light_intensity", cJSON_CreateNumber(esp32_state.light_intensity));
    cJSON_AddItemToObject(json_reported, "message", cJSON_CreateString(esp32_state.message));
    cJSON_AddItemToObject(json_reported, "motor", cJSON_CreateNumber(esp32_state.motor));
    cJSON_AddItemToObject(json_reported, "temperature", cJSON_CreateNumber(esp32_state.temperature));

    cJSON_AddNullToObject(json_state, "desired");
    cJSON_AddItemToObject(json_state, "reported", json_reported);

    cJSON_AddItemToObject(json_shadow, "state", json_state);

    cjson_str = cJSON_Print(json_shadow);
    printf("build json:\n%s\n", cjson_str);

    cjson_str = cJSON_PrintUnformatted(json_shadow);

    if (strlen(cjson_str) >= len)
    {
        printf("build unformatted json to long! len:%d buf len:%d\n", strlen(cjson_str), len);
        cJSON_Delete(json_shadow);
        return false;
    }

    printf("build unformatted json len:%d\n", strlen(cjson_str));

    strcpy(out_string, cjson_str);
    cJSON_Delete(json_shadow);

    return true;
}

esp_err_t parse_text_json(char *json_msg, char *text, uint8_t *speed, uint32_t *count)
{
    esp_err_t ee = ESP_FAIL;
    cJSON *json, *json_text, *json_speed, *json_count;
    json = cJSON_Parse(json_msg);
    if (cJSON_IsObject(json))
    {
        json_text = cJSON_GetObjectItem(json, "text");
        json_speed = cJSON_GetObjectItem(json, "speed");
        json_count = cJSON_GetObjectItem(json, "count");
        // 类型判断
        if (!cJSON_IsString(json_text) ||
            !cJSON_IsNumber(json_speed) ||
            !cJSON_IsNumber(json_count))
        {
            ESP_LOGW(TAG, "parse_text_json 类型解析错误");
        }
        else
        {
            sprintf(text, "%s", json_text->valuestring);
            speed[0] = json_speed->valueint;
            count[0] = json_count->valueint;
            ee = ESP_OK;
        }
    }
    cJSON_Delete(json);
    return ee;
}

/*
    函数功能：构建json格式ota数据字符串
    函数名字：build_ota_string
    输出：*out_string       输出无格式json字符串
    输入：len              *out_string缓存长度
    返回：成功  true    失败    false
*/
int build_ota_string(char *out_string, int len)
{
    char *cjson_str = NULL;

    cJSON *json_body, *json_report;
    json_body = cJSON_CreateObject();
    json_report = cJSON_CreateObject();

    cJSON_AddItemToObject(json_report, "version", cJSON_CreateString(FIRMWARE_VERSION));

    cJSON_AddItemToObject(json_body, "type", cJSON_CreateString("report_version"));
    cJSON_AddItemToObject(json_body, "report", json_report);

    cjson_str = cJSON_Print(json_body);
    printf("build json:\n%s\n", cjson_str);

    cjson_str = cJSON_PrintUnformatted(json_body);

    if (strlen(cjson_str) >= len)
    {
        printf("build unformatted json to long! len:%d buf len:%d\n", strlen(cjson_str), len);
        cJSON_Delete(json_body);
        return false;
    }

    printf("build unformatted json len:%d\n", strlen(cjson_str));

    strcpy(out_string, cjson_str);
    cJSON_Delete(json_body);

    return true;
}